$().ready(function() {



	loadWorkerType();

	loadDep();

	validateRule();
	var gender =  $("#gender").val();
    	if(gender=='男'){
    	    $("#genderM").prop('checked', true);//radio默认选中
    	}else{
    	    $("#genderF").prop('checked', true);//radio默认选中
    	}
});
$('#birthDate').datepicker({
	   format:"yyyy-mm-dd",
             startData:"0",
             language:"zh-CN",
             autoclose:true
	});
$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
function update() {
    //性别单选
    var val =$("input[type='radio']:checked").val();
    $("#gender").val(val);
	$.ajax({
		cache : true,
		type : "POST",
		url : "/medicalworker/update",
		data : $('#workerForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#workerForm").validate({
		rules : {
			workerName : {
				required : true
			}
		},
		messages : {
			workerName : {
				required : icon + "请输入名字"
			}
		}
	})
}

function loadWorkerType(){
	var html = "";
	$.ajax({
		url : '/medicalworker/workerType',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].value + '">' + data[i].name + '</option>'
			}
			$("#medicalWorkerType").append(html);
			$("#medicalWorkerType").chosen({
				maxHeight : 200
			});
			$("#medicalWorkerType").val($("#medicalWorkerType2").val());
			alert($("#medicalWorkerType2").val());
            $("#medicalWorkerType").trigger("chosen:updated");
            workerTitleList($("#medicalWorkerType2").val());
			//工作人员类型点击事件
            $('#medicalWorkerType').on('change', function(e, params) {
                workerTitleList(params.selected);
            });


		}
	});
}



function workerTitleList(workerType){
	var html = "";
	$.ajax({
		url : '/medicalworker/workerTitleList',
		data : {
        		'workerType' : workerType+"_title"
        },
		success : function(data) {
		    $("#title option").remove();
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].value + '">' + data[i].name + '</option>'
			}
			$("#title").append(html);
			$("#title").val($("#title2").val());
            $("#title").trigger("chosen:updated");
        }
    });
    $("#title option").remove();
}

function loadDep(){
	var html = "";
	$.ajax({
		url : '/medicalworker/workerDepList',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].deptId + '">' + data[i].name + '</option>'
			}
			$("#deptId").append(html);
			$("#deptId").chosen({
				maxHeight : 200
			});
	        $("#deptId").val($("#deptId2").val());
            $("#deptId").trigger("chosen:updated");

		}
	});




}